extends State
class_name Playerwalk

var gravity:float = Autoload.gravity
var direction:float = 0
@export var max_velocity:float = 400.0
@onready var player_sprite: AnimatedSprite2D = $"../../PlayerSprite"
var walk_animation_time:float = 0.5
@export var walk_acceleration:float = 2000.0
@export var ground_friction:float = 1500.0


func enter():
	print("进入行走阶段") #测试用，可删
	walk_animation_time = 0.05
	
func update(delta:float):
	player_sprite.play("walk")
	if is_zero_approx(character.velocity.x):
		walk_animation_time -= delta
	if walk_animation_time <= 0 or Autoload.stage_show:
		transition_requested.emit("Playeridle")
	if InputBuffer.has_buffered_input("玩家跳跃"):
		InputBuffer.consume_buffered_input("玩家跳跃")
		transition_requested.emit("Playerjump")
	#if InputBuffer.has_buffered_input("冲刺") and Autoload.dashflag == true:
		#InputBuffer.consume_buffered_input("冲刺")
		#transition_requested.emit("Playerdash")
		
func physics_update(delta:float):
	if character.is_on_floor():
		if Autoload.dashflag == false:
			Autoload.dashflag = true
		Autoload.wolf_time = Autoload.wolf_time_stander
	if !character.is_on_floor():
		Autoload.wolf_time -= delta
	direction = Input.get_axis("玩家向左走","玩家向右走")
	character.velocity.y += gravity * delta
	if (direction > 0):
		player_sprite.flip_h = 0
	if (direction < 0):
		player_sprite.flip_h = 1
		
	if direction:
		character.velocity.x += direction * walk_acceleration * delta
		character.velocity.x = clamp(character.velocity.x,-max_velocity,max_velocity)
	else:
		if character.velocity.x > 0:
			character.velocity.x = max(0, character.velocity.x - ground_friction * delta)
		elif character.velocity.x < 0:
			character.velocity.x = min(0, character.velocity.x + ground_friction * delta)
	character.move_and_slide()
		
		
	
